package gui;

import group.UserGroup;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

import javax.swing.AbstractListModel;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import role.Role;
import role.RoleFactory;
import seminar.Seminar;

@SuppressWarnings("serial")
public class EditRolePane extends JLayeredPane implements ActionListener
{
	public JPanel contentPane;
	public JTextField rolename_field;
	public JTextArea comment_area;
	private JButton save_button;
	private JButton delete_button;
	private JButton new_role_button;
	private JList<String> list;

	public EditRolePane()
	{

		this.setLayout(new BorderLayout(0, 0));
		JPanel left_panel = new JPanel();
		this.add(left_panel, BorderLayout.WEST);
		left_panel.setLayout(new BorderLayout(0, 0));

		JPanel inner_left_panel = new JPanel();
		left_panel.add(inner_left_panel, BorderLayout.CENTER);
		inner_left_panel.setLayout(new BorderLayout(0, 0));

		list = new JList<String>();
		updateList();

		list.addMouseListener(fillFields());
		inner_left_panel.add(list, BorderLayout.CENTER);

		JPanel southwest_button_panel = new JPanel();
		inner_left_panel.add(southwest_button_panel, BorderLayout.SOUTH);
		southwest_button_panel.setLayout(new BorderLayout(0, 0));

		delete_button = new JButton("Löschen");
		delete_button.addActionListener(this); // Button wird dem Listener
												// zugeordnet
		delete_button.setFont(new Font("Tahoma", Font.PLAIN, 12));
		delete_button.setEnabled(false);
		southwest_button_panel.add(delete_button, BorderLayout.WEST);

		new_role_button = new JButton("Neue Rolle");
		new_role_button.addActionListener(this); // Button wird dem Listener
													// zugeordnet
		new_role_button.setFont(new Font("Tahoma", Font.PLAIN, 12));
		southwest_button_panel.add(new_role_button, BorderLayout.EAST);

		JPanel panel = new JPanel();
		southwest_button_panel.add(panel, BorderLayout.CENTER);

		Component horizontalStrut = Box.createHorizontalStrut(20);
		panel.add(horizontalStrut);

		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		panel.add(horizontalStrut_1);

		JLabel lblRollen = new JLabel("Rollen:");
		inner_left_panel.add(lblRollen, BorderLayout.NORTH);

		Component horizontalStrut_3 = Box.createHorizontalStrut(20);
		left_panel.add(horizontalStrut_3, BorderLayout.WEST);

		Component verticalStrut_5 = Box.createVerticalStrut(20);
		left_panel.add(verticalStrut_5, BorderLayout.NORTH);

		JPanel right_panel = new JPanel();
		this.add(right_panel, BorderLayout.CENTER);
		right_panel.setLayout(new BorderLayout(0, 0));

		JPanel inner_right_panel = new JPanel();
		right_panel.add(inner_right_panel, BorderLayout.CENTER);
		inner_right_panel.setLayout(new BorderLayout(0, 0));

		JPanel rolename_panel = new JPanel();
		inner_right_panel.add(rolename_panel, BorderLayout.NORTH);
		rolename_panel.setLayout(new BorderLayout(0, 0));

		Component horizontalStrut_12 = Box.createHorizontalStrut(20);
		rolename_panel.add(horizontalStrut_12, BorderLayout.WEST);

		Component horizontalStrut_13 = Box.createHorizontalStrut(20);
		rolename_panel.add(horizontalStrut_13, BorderLayout.EAST);

		JPanel panel_4 = new JPanel();
		rolename_panel.add(panel_4, BorderLayout.CENTER);
		panel_4.setLayout(new BorderLayout(0, 0));

		this.rolename_field = new JTextField();
		panel_4.add(this.rolename_field, BorderLayout.CENTER);
		this.rolename_field.setColumns(10);

		JLabel rolename_label = new JLabel("Rollenname: ");
		rolename_label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_4.add(rolename_label, BorderLayout.NORTH);

		Component verticalStrut_2 = Box.createVerticalStrut(20);
		rolename_panel.add(verticalStrut_2, BorderLayout.SOUTH);

		Component verticalStrut_3 = Box.createVerticalStrut(20);
		rolename_panel.add(verticalStrut_3, BorderLayout.NORTH);

		JPanel comment_panel = new JPanel();
		inner_right_panel.add(comment_panel, BorderLayout.CENTER);
		comment_panel.setLayout(new BorderLayout(0, 0));

		Component horizontalStrut_10 = Box.createHorizontalStrut(20);
		comment_panel.add(horizontalStrut_10, BorderLayout.WEST);

		Component horizontalStrut_11 = Box.createHorizontalStrut(20);
		comment_panel.add(horizontalStrut_11, BorderLayout.EAST);

		JPanel panel_5 = new JPanel();
		comment_panel.add(panel_5, BorderLayout.CENTER);
		panel_5.setLayout(new BorderLayout(0, 0));

		JLabel comment_label = new JLabel("Kommentar: ");
		comment_label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_5.add(comment_label, BorderLayout.NORTH);

		comment_area = new JTextArea();
		panel_5.add(comment_area);
		comment_area.setLineWrap(true);

		Component verticalStrut_1 = Box.createVerticalStrut(20);
		comment_panel.add(verticalStrut_1, BorderLayout.SOUTH);

		JPanel southeast_button_panel = new JPanel();
		inner_right_panel.add(southeast_button_panel, BorderLayout.SOUTH);
		southeast_button_panel.setLayout(new BorderLayout(0, 0));

		save_button = new JButton("Speichern");
		save_button.setEnabled(false);
		save_button.addActionListener(this); // Button wird dem Listener
												// zugeordnet
		save_button.setFont(new Font("Tahoma", Font.PLAIN, 12));
		southeast_button_panel.add(save_button, BorderLayout.CENTER);

		Component verticalStrut = Box.createVerticalStrut(20);
		southeast_button_panel.add(verticalStrut, BorderLayout.SOUTH);

		JPanel panel_2 = new JPanel();
		southeast_button_panel.add(panel_2, BorderLayout.EAST);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		Component horizontalStrut_5 = Box.createHorizontalStrut(20);
		panel_2.add(horizontalStrut_5);

		Component horizontalStrut_7 = Box.createHorizontalStrut(20);
		panel_2.add(horizontalStrut_7);

		Component horizontalStrut_9 = Box.createHorizontalStrut(20);
		panel_2.add(horizontalStrut_9);

		Component horizontalStrut_15 = Box.createHorizontalStrut(20);
		panel_2.add(horizontalStrut_15);

		JPanel panel_3 = new JPanel();
		southeast_button_panel.add(panel_3, BorderLayout.WEST);
		panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		Component horizontalStrut_4 = Box.createHorizontalStrut(20);
		panel_3.add(horizontalStrut_4);

		Component horizontalStrut_6 = Box.createHorizontalStrut(20);
		panel_3.add(horizontalStrut_6);

		Component horizontalStrut_8 = Box.createHorizontalStrut(20);
		panel_3.add(horizontalStrut_8);

		Component horizontalStrut_14 = Box.createHorizontalStrut(20);
		panel_3.add(horizontalStrut_14);

		Component horizontalStrut_2 = Box.createHorizontalStrut(20);
		right_panel.add(horizontalStrut_2, BorderLayout.WEST);

	}

	private void updateList()
	{

		Role r;
		ArrayList<Role> role = RoleFactory.getRoles();
		final String[] values = new String[role.size()];
		int counter = 0;
		while (counter < role.size())
		{
			r = role.get(counter);
			values[counter] = r.getName();
			counter++;
		}

		list.setModel(new AbstractListModel() {
			
			@Override
			public int getSize()
			{
				return values.length;
			}

			@Override
			public Object getElementAt(int index)
			{
				return values[index];
			}
		});

	}

	private MouseListener fillFields()
	{
		return new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent evt)
			{
				ArrayList<Role> role = RoleFactory.getRoles();
				if (list.getSelectedIndex() >= 0)
				{
					Role r = role.get(list.getSelectedIndex());
					rolename_field.setText(r.getName());
					comment_area.setText(r.getComment());
					save_button.setEnabled(true);
					delete_button.setEnabled(true);
				}
			}
		};

	}

	@Override
	public void actionPerformed(ActionEvent event)
	{

		if (event.getSource() == this.save_button)
		{
			GUI.statusausgabe("Rolle wird gespeichert.");
			save_button.setEnabled(false);
			delete_button.setEnabled(false);
			
			
			Role newrole = new Role(rolename_field.getText());
			newrole.setComment(comment_area.getText());
			if(RoleFactory.contains(newrole))
			{				
				GUI.statusausgabe("Rollenname bereits verwendet");
				
			}else
			{
				
				RoleFactory.replace(RoleFactory.getRoles().get(list.getSelectedIndex()),newrole);	
				int i=0;
				Iterator<UserGroup> groupsIter = Seminar.getUsergoups().iterator();
				ListIterator<Role> roleIter;
			
				while (groupsIter.hasNext()) {
					roleIter = groupsIter.next().getRoles().listIterator();
					while (roleIter.hasNext()) {
						
						if (roleIter.next().getName().equals(list
								.getSelectedValue().toString())) {
							roleIter.previous();
							roleIter.next().setName(rolename_field.getText());
						}						
					}
				}
			}
			updateList();
		}

		if (event.getSource() == this.delete_button)
		{
			deletRole();
		}
		if (event.getSource() == this.new_role_button)
		{
			GUI.statusausgabe("Rolle wird angelegt.");
			Role r = new Role("<neue Rolle>");
			RoleFactory.addRole(r);
			save_button.setEnabled(false);
			delete_button.setEnabled(false);
			updateList();
		}
	}

	private void deletRole()
	{
		RoleFactory.getRoles().remove(list.getSelectedIndex());
		rolename_field.setText(null);
		comment_area.setText(null);
		GUI.statusausgabe("Rolle wird gel�scht.");
		save_button.setEnabled(false);
		delete_button.setEnabled(false);
		updateList();
	}

	// private String[] getAllRolenames() {
	// Role r;
	// ArrayList<Role> role = RoleFactory.getRoles();
	// String[] values = new String[role.size()];
	// int counter = 0;
	// while (counter < role.size()) {
	// r = role.get(counter);
	// values[counter] = r.getName();
	// }
	// return values;
	// }

}
